package com.ruoyi.inspection.utils;

import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.Method;
import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class HttpClientUtil {

//                .header(Header.CONTENT_TYPE, "multipart/form-data; boundary=<calculated when request is sent>")
//                .header(Header.ACCEPT, "*/*")

    public static void main(String[] args) {
        Map<String, Object> requestMap = new HashMap<>();
        requestMap.put("key", "value");
    }

    public static Map<String, Object> sendRequestForJsonData(String url, Map<String, Object> requestMap, Method method) {
        HttpRequest httpRequest = new HttpRequest(url)
                .header(Header.CONTENT_TYPE, "application/json; charset=UTF-8")
                .header(Header.ACCEPT, "application/json")
                .header(Header.ACCEPT_ENCODING, "");

        httpRequest.method(method);
        httpRequest.body(JSONObject.toJSONString(requestMap));

        // 执行请求
        HttpResponse response = httpRequest.execute();
        Map map = new HashMap<String, Object>();
        try {
            String body = response.body();
            map = new ObjectMapper().readValue(body, Map.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return map;
    }


    public static String sendRequestData(String url, String params, Method method) {
        HttpRequest httpRequest = new HttpRequest(url)
                .header(Header.CONTENT_TYPE, "application/json; charset=UTF-8")
                .header(Header.ACCEPT, "application/json")
                .header(Header.ACCEPT_ENCODING, "UTF-8");

        httpRequest.method(method);
        httpRequest.body(params);

        // 执行请求
        HttpResponse response = httpRequest.execute();
        return response.body();
    }
}

